package org.jeecg.modules.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.pay.entity.ResourcesUsage;
import org.jeecg.modules.pay.mapper.ResourcesUsageMapper;
import org.jeecg.modules.pay.service.IResourcesUsageService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 资源用量
 * @Author: jeecg-boot
 * @Date:   2022-06-14
 * @Version: V1.0
 */
@Service
public class ResourcesUsageServiceImpl extends ServiceImpl<ResourcesUsageMapper, ResourcesUsage> implements IResourcesUsageService {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public IPage<ResourcesUsage> getPage(Integer pageNo, Integer pageSize, QueryWrapper<ResourcesUsage> queryWrapper) {
        Page<ResourcesUsage> page = new Page<ResourcesUsage>(pageNo, pageSize);
        queryWrapper.orderByDesc("date_time");
        return page(page, queryWrapper);
    }

    @Override
    public void add(ResourcesUsage resourcesUsage) {
        save(resourcesUsage);
    }

    @Override
    public void edit(ResourcesUsage resourcesUsage) {
        updateById(resourcesUsage);
    }

    @Override
    public void deleteId(String id) {
        removeById(id);
    }

    @Override
    public void removes(List<String> asList) {
        removeByIds(asList);
    }

    @Override
    public List<ResourcesUsage> selectByName(String name) {
        ArrayList<ResourcesUsage> resourcesUsages = new ArrayList<>();
        QueryWrapper<SysUser> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("realname", name);
        List<SysUser> list = sysUserService.list(objectQueryWrapper);
        for (SysUser sysUser : list) {
            resourcesUsages.add(getById(sysUser.getId()));
        }
        return resourcesUsages;
    }
}
